package org.social.remote;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.bean.AccountInfo;
import org.bean.ChatRoom;
import org.bean.DungeonTeam;
import org.bean.FriendInfo;
import org.bean.GiftCodeDTO;
import org.bean.PlayerInfo;
import org.bean.StoreInfo;
import org.bean.SourceChatMsg;
import org.constant.ServerTypeEnum;
import org.remote.social.RemoteSocialServer;
import org.social.db.DbSave;
import org.social.db.bean.PlayerMap;
import org.social.db.cache.PlayerMapCache;
import org.social.manager.ActivityManager;
import org.social.manager.DictionariesManager;
import org.social.manager.FunctionSwitchManager;
import org.social.manager.GiftCodeManager;
import org.social.manager.InterceptManager;
import org.social.manager.NoticeManager;
import org.social.manager.ServerStatusManager;
import org.social.manager.StoreManager;
import org.social.module.ChatManager;
import org.social.module.CommodityManager;
import org.social.module.FriendManager;
import org.social.module.PlayerManager;
import org.social.module.SocialManager;

import com.sojoys.artifact.annotation.RemoteServer;
import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

/**
 * @author : DengYing
 * @CreateDate : 2017年8月14日 下午3:16:39
 * @Description ：Please describe this document
 */
@RemoteServer(RemoteSocialServer.class)
public class RemoteSocialServerImpl implements RemoteSocialServer {
	static final Log log = LogFactory.get(RemoteSocialServerImpl.class);
	
	@Override
	public void loginServerStart() {
		// 屏蔽信息 -> 登陆服务器
		InterceptManager.notifyRemote(ServerTypeEnum.LOGIN_SERVER,null);
		// 服务器开关 -> 登陆服务器
		FunctionSwitchManager.notifyRemote(ServerTypeEnum.LOGIN_SERVER,null);
	}

	@Override
	public void gameServerStart(String gameServerId) {
		// 屏蔽信息 -> 游戏服务器
		InterceptManager.notifyRemote(ServerTypeEnum.GAME_SERVER,gameServerId);
		// 服务器开关 -> 游戏服务器
		FunctionSwitchManager.notifyRemote(ServerTypeEnum.GAME_SERVER,gameServerId);
		// 活动信息 -> 游戏服务器
		ActivityManager.me().notifyRemote(gameServerId);
		// 字典信息 -> 游戏服务器
		DictionariesManager.notifyRemote(ServerTypeEnum.GAME_SERVER,gameServerId);
		// 商城信息 -> 游戏服务器
		StoreManager.me().notifyRemote(ServerTypeEnum.GAME_SERVER,gameServerId);
	    // 公告信息 -> 游戏服务器
        NoticeManager.notifyRemote(gameServerId);
	}
	
	@Override
	public int[] getBatchPidPart(int count) throws AbstractLogicModelException {
		return PlayerMapCache.me().getPlayerMapCacheProxy().getBatchPidPart(count);
	}

	@Override
	public void createPlayerMap(AccountInfo accountInfo,PlayerInfo playerInfo) {
		PlayerManager.me().createPlayerMap(accountInfo,playerInfo);
	}
	

	@Override
	public void sendChat(SourceChatMsg msg) throws AbstractLogicModelException {
		ChatManager.me().sendChat(msg);
	}


	@Override
	public ChatRoom enterChatRoom(int playerId) throws AbstractLogicModelException {
		ChatRoom room = ChatManager.me().enterChatRoom(playerId,0,0);
		LinkedList<SourceChatMsg> msgs = room.getAllMsg();
		for (SourceChatMsg sourceChatMsg : msgs) {
			PlayerMap sourcePlayerMap = PlayerMapCache.me().getByPid(sourceChatMsg.getSourcePid());
			if (sourcePlayerMap!=null) {
				sourceChatMsg.setLvl(sourcePlayerMap.getPlayerMapProxy().getPlv());
				sourceChatMsg.setHelpFightHeroCid(sourcePlayerMap.getPlayerMapProxy().getHelpHeroCid());
			}
		}
		return room;
	}

	@Override
	public ChatRoom changeRoomNumber(int chatRoomId,int playerId) throws AbstractLogicModelException {
		return ChatManager.me().changeChatRoom(chatRoomId,playerId);
	}

	@Override
	public List<Long> getIds() {
		return SocialManager.me().getIds();
	}


	@Override
	public void addCommodityBuyRecord(int commodityId, int num) throws AbstractLogicModelException {
		CommodityManager.me().addCommodityBuyRecord(commodityId, num);
	}


	@Override
	public void exitChatRoom(int pid) throws AbstractLogicModelException {
		ChatManager.me().exitChatRoom(pid);
	}

	@Override
	public void uploadPlayerInfo(int pid,String serverId,Map<Integer, Object> map) throws AbstractLogicModelException {
		// 修改玩家信息
		PlayerManager.me().changePlayerInfo(pid,serverId, map);
	}

	@Override
	public Map<Integer,PlayerInfo> getHelpPlayers(int pid,int count,List<Integer> filterPids) {
		return PlayerManager.me().getHelpPlayers(pid, count, filterPids);
	}



	@Override
	public PlayerInfo getPlayerInfo(int pid) throws AbstractLogicModelException {
		return PlayerManager.me().getPlayerInfo(pid);
	}


	@Override
	public List<FriendInfo> getFriends(int pid) throws AbstractLogicModelException {
		return FriendManager.me().getFriends(pid);
	}

	@Override
	public void operate(int pid, List<Integer> targetIds, int type,Map<Object, Object> ext) throws AbstractLogicModelException {
		FriendManager.me().operate(pid, targetIds, type, ext);
	}

	@Override
	public List<PlayerInfo> getRecommendFriends(int pid) throws AbstractLogicModelException {
		return FriendManager.me().getRecommendFriends(pid);
	}

	@Override
	public DungeonTeam createTeam(PlayerInfo playerInfo, int levelCid, boolean matching) {
		return null;
	}

	@Override
	public DungeonTeam matching(PlayerInfo playerInfo, int levelCid) {
		return null;
	}

	@Override
	public boolean invite(PlayerInfo playerInfo, int levelCid) {
		return false;
	}

	@Override
	public GiftCodeDTO gainGiftCode(int pid,String osName,int channelId,String code) {
		return GiftCodeManager.gainGift(pid, osName, channelId, code);
	}

	@Override
	public List<Integer> getFunctionSwitch(int channelId, int channelAppId) {
		return null;
	}

	@Override
	public void saveDB() {
		try {
			DbSave.saveAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void uploadActivityEntryStatus(int activityId, List<Integer> entryIds, int pid, int status) {
		ActivityManager.me().uploadActivityEntryStatus(activityId,entryIds,pid,status);
	}

	@Override
	public String getUid(int pid) {
		PlayerMap pm = PlayerMapCache.me().getByPid(pid);
		if (pm == null){
			return null;
		}
		return pm.getUid();
	}

    @Override
    public int getStatus() {
        return ServerStatusManager.me().getServerStatus();
    }

	@Override
	public void rewardFriendPoint(int pid, int helpPlayerId) {
		FriendManager.me().rewardFriendShipPointByHelpFight(pid,helpPlayerId);
	}
}
